﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using VisionCloud.Core.Model.Ms;
using VisionCloud.Domain.DtoModel.Ms;
using VisionCloud.Domain.Repository.Interface.Ms;

namespace VisionCloud.Domain.Repository.Implement.Ms
{
    public class MSTaskPoint2ManualMeasurementRepository : BaseRepository<MSTaskPoint2ManualMeasurement>, IMSTaskPoint2ManualMeasurementRepository
    {
        public new async Task<List<MSTaskPoint2ManualMeasurement>> Get(Guid taskId, Guid pointId)
        {
            return await (
                from h in Db.MSTaskPoint2ManualMeasurements
                where h.TaskId == taskId && h.PointId == pointId
                select h).AsNoTracking().ToListAsync().ConfigureAwait(false);
        }

        public async Task<List<MSTaskPoint2ManualMeasurement>> GetMeasurement2PointDto(Guid taskId)
        {
            return await (
                 from h in Db.MSTaskPoint2ManualMeasurements
                 where h.TaskId == taskId
                 select h).AsNoTracking().ToListAsync().ConfigureAwait(false);
        }

        public async Task<MSTaskPoint2ManualMeasurement> getModelByTaskAndPoint(Guid pointId, string strType)
        {
            return await Db.MSTaskPoint2ManualMeasurements

               .Where(p => p.PointId == pointId && p.Code == strType)
               .Select(r => r).AsNoTracking().SingleOrDefaultAsync().ConfigureAwait(false);
        }

        [Obsolete("该不再需要该方法")]
        public async Task<List<MSManualMeasurement2PointDto>> GetPointMeasureItems(Guid taskId, Guid pointId)
        {
            return await (
              from h in Db.MSTaskPoint2ManualMeasurements
              join a in Db.MsTaskMeasureItemTemplates on h.MsTaskMeasureItemTemplateId equals a.MsTaskMeasureItemTemplateId
              where h.TaskId == taskId && h.PointId == pointId
              select new MSManualMeasurement2PointDto()
              {
                  MSTaskPoint2ManualMeasurementId = h.MSTaskPoint2ManualMeasurementId,
                  PointId = h.PointId,
                  Code = a.Code,
                  Title = a.Title,
                  ManualMeasurementTypeId = a.Id,
                  Unit = a.Unit,
                  DesignValues = h.ValueDesign,   // 设计值
                  Values = h.Values,              // 实测值
                  Type = a.Type
              }).AsNoTracking().ToListAsync().ConfigureAwait(false);
        }

        public async Task<List<MSManualMeasurement2PointDto>> GetPointMeasureItems(Guid taskId, int pointIndex)
        {
            return await (
              from h in Db.MSTaskPoint2ManualMeasurements
              join i in Db.TaskPointss on new { TaskId = h.TaskId, PointId = h.PointId } equals new { TaskId = i.TaskId, PointId = i.pointId }
              join a in Db.MsTaskMeasureItemTemplates on h.MsTaskMeasureItemTemplateId equals a.MsTaskMeasureItemTemplateId into t
              where h.TaskId == taskId && i.pintIndex == pointIndex
              from tt in t.DefaultIfEmpty()
              select new MSManualMeasurement2PointDto()
              {
                  MSTaskPoint2ManualMeasurementId = h.MSTaskPoint2ManualMeasurementId,
                  PointId = h.PointId,
                  Code = !string.IsNullOrWhiteSpace(tt.Code) ? tt.Code : h.Code,
                  Title = !string.IsNullOrWhiteSpace(tt.Title) ? tt.Title : h.Title,
                  ManualMeasurementTypeId = !string.IsNullOrWhiteSpace(tt.Id) ? tt.Id : h.ManualMeasurementTypeId,
                  Unit = !string.IsNullOrWhiteSpace(tt.Unit) ? tt.Unit : h.Unit,
                  DesignValues = h.ValueDesign,   // 设计值
                  Values = h.Values,              // 实测值
                  Type = tt.Type
              }).AsNoTracking().ToListAsync().ConfigureAwait(false);
        }

        /// <summary>
        /// 站点的人工测量项是否录入完毕
        /// </summary>
        /// <param name="pointId">站点Id</param>
        /// <returns></returns>
        public async Task<bool> IsManualMeasureItemsCompleted(Guid pointId)
        {
            var result = await (
                from h in Db.MSTaskPoint2ManualMeasurements
                where h.PointId == pointId && h.Code != "r-kaijianjinshenpiancha"
                select h).AsNoTracking().ToListAsync().ConfigureAwait(false);
            if (result == null || result.Count == 0)
            {
                return true;
            }
            var uncompleted = result.Where(
                x => string.IsNullOrWhiteSpace(x.Values)
                || string.Equals(x.Values, "[]")).ToList();
            if (uncompleted != null && uncompleted.Count > 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 录入人工测量项否
        /// </summary>
        /// <param name="taskId">任务Id</param>
        /// <returns></returns>
        public async Task<bool> HasMeasureItemInputData(Guid taskId)
        {
            var result = await (
                from h in Db.MSTaskPoint2ManualMeasurements
                where h.TaskId == taskId && h.Code != "r-kaijianjinshenpiancha"
                select h).AsNoTracking().ToListAsync().ConfigureAwait(false);
            if (result == null || result.Count == 0)
            {
                return false;
            }
            var hasInputData = result.Where(
                x => !string.IsNullOrWhiteSpace(x.Values)
                && string.Equals(x.Values, "[]")).ToList();
            if (hasInputData != null && hasInputData.Count > 0)
            {
                return true;
            }
            return false;
        }
    }
}